﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Module.Core.LoginSystem.Commons;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.LoginSystem.Commons.Utilities;
using Module.Core.LoginSystem.Components.Commands;
using Module.Core.LoginSystem.Components.Roles;

namespace Web.Cms.Roles
{
    public partial class RolePermisions : AdminUserControl
    {
        private int roleId;
        public string strCommands;
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            CommandInfo _commandInfo = CommandController.GetCommand("admin_role");
            RolePermisionInfo _roleInfo =
            RoleController.GetReadWritePermison(CurrentUser.UserID, _commandInfo.CommandID);
            if (_roleInfo == null) return;
            if (_roleInfo.Write == 0)
            {

                btnUpdate.Visible = false;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            lblUpdateStatus.Text = string.Empty;
            roleId = ConvertUtility.ToInt32(Request.QueryString["roleid"]);
            if (!IsPostBack)
            {

                RoleInfo role = RoleController.GetRole(roleId);
                if (role == null)
                {
                    this.Visible = false;
                    return;
                }
                lblGroup.Text = role.RoleName;
                List<CommandItem> lstCommandItems = new List<CommandItem>();
                List<CommandInfo> lstRootCommands = CommandController.GetCommands(CurrentPortalID, 0);
                foreach (CommandInfo command in lstRootCommands)
                {
                    CommandItem item = new CommandItem(command.Title, command.CommandID, command.IsVisible);

                    item.Level = 0;
                    lstCommandItems.Add(item);
                    LoadCurItem(lstCommandItems, item);
                }
                grdCommands.DataSource = lstCommandItems;
                grdCommands.RowDataBound += new GridViewRowEventHandler(grdCommands_RowDataBound);
                grdCommands.DataBind();
            }
        }

        protected void grdCommands_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                CommandItem _commandInfo = (CommandItem)e.Row.DataItem;
                if (_commandInfo.IsVisible == false)
                {
                    e.Row.Visible = false;
                    return;
                }
                Label lblCommandId = (Label)e.Row.FindControl("lblCommandId");
                int commandID = ConvertUtility.ToInt32(lblCommandId.Text);
                strCommands = "";
                GetAllCurItemToString(commandID);
                if (strCommands.Length > 0)
                {
                    e.Row.Cells[0].Style.Add("background", "#379DEF");
                    e.Row.Cells[0].Style.Add("color", "Black");
                }

                CheckBox chkRead = (CheckBox)e.Row.FindControl("chkRead");
                //chkRead.Attributes.Add("commandid", commandID.ToString());
                chkRead.Attributes.Add("onclick", "GetRole('" + grdCommands.ClientID + "','" + chkRead.ClientID + "','" + strCommands + "')");
                CheckBox chkWrite = (CheckBox)e.Row.FindControl("chkWrite");
                chkWrite.Attributes.Add("onclick", "GetRole('" + grdCommands.ClientID + "','" + chkWrite.ClientID + "','" + strCommands + "')");
                //chkWrite.Attributes.Add("commandid", commandID.ToString());
                int _commandid = Convert.ToInt32(lblCommandId.Text);
                RolePermisionInfo permis = CommandController.GetRolePermision(roleId, _commandid);

                if (permis != null)
                {
                    if (_commandid == 48)
                    {
                        chkRead.Checked = true;
                        chkWrite.Checked = true;
                        e.Row.Visible = false;
                    }
                    else
                    {
                        chkRead.Checked = permis.Read > 0;
                        chkWrite.Checked = permis.Write > 0;
                    }

                }
                else
                {

                    if (_commandid == 48)
                    {
                        chkRead.Checked = true;
                        chkWrite.Checked = true;
                        e.Row.Visible = false;
                    }
                    else
                    {
                        if (_commandid == 17 || _commandid == 18)
                        {
                            chkRead.Checked = true;
                            chkRead.Enabled = false;
                            chkWrite.Enabled = true;
                            chkWrite.Checked = false;
                            return;
                        }

                        if (_commandid == 19)
                        {
                            chkRead.Checked = true;
                            chkWrite.Checked = false;
                            return;
                        }
                        else
                        {
                            chkRead.Checked = false;
                            chkWrite.Checked = false;
                        }
                        //chkRead.Checked = chkWrite.Checked = false;
                    }
                }
            }
        }
        protected void GetAllCurItemToString(int _curCommandID)
        {
            List<CommandInfo> lstChildCommands = CommandController.GetCommands(CurrentPortalID, _curCommandID);
            foreach (CommandInfo command in lstChildCommands)
            {
                strCommands += command.CommandID + "_";
                GetAllCurItemToString(command.CommandID);
            }
        }

        protected void LoadCurItem(List<CommandItem> lstCommandItems, CommandItem curItem)
        {
            List<CommandInfo> lstChildCommands = CommandController.GetCommands(CurrentPortalID, curItem.ID);
            int level = curItem.Level;
            level++;
            foreach (CommandInfo command in lstChildCommands)
            {
                CommandItem item = new CommandItem(MiscUtility.StringIndent(level) + command.Title, command.CommandID, command.IsVisible);
                item.Level = level;

                lstCommandItems.Add(item);

                LoadCurItem(lstCommandItems, item);
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (GridViewRow row in grdCommands.Rows)
                {

                    CheckBox chkRead = (CheckBox)row.FindControl("chkRead");
                    CheckBox chkWrite = (CheckBox)row.FindControl("chkWrite");
                    Label lblCommandId = (Label)row.FindControl("lblCommandId");
                    CommandInfo _commandInfo = CommandController.GetCommand(ConvertUtility.ToInt32(lblCommandId.Text));

                    if (_commandInfo.IsVisible == false)
                    {
                        CommandController.AddPermisionToRole(roleId, Convert.ToInt32(lblCommandId.Text), true,
                                                             false);
                    }
                    else
                    {
                        if (chkRead.Checked || chkWrite.Checked)
                        {
                            CommandController.AddPermisionToRole(roleId, Convert.ToInt32(lblCommandId.Text), chkRead.Checked,
                                                                 chkWrite.Checked);
                        }
                        else
                        {
                            CommandController.RemovePermisionFromRole(roleId, ConvertUtility.ToInt32(lblCommandId.Text));
                        }
                    }

                }
                lblUpdateStatus.Text = Definitions.MSG_UPDATE_SUCCESS;
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = ex.Message;
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in grdCommands.Rows)
            {
                CheckBox chkRead = (CheckBox)row.FindControl("chkRead");
                CheckBox chkWrite = (CheckBox)row.FindControl("chkWrite");
                chkRead.Checked = chkWrite.Checked = false;
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            if (roleId != 0)
            {
                Response.Redirect(
                        UIProcess.GetCPPage(CurrentPortalID, "admin_role", new string[] { }, new string[] { }));
            }
            else
            {
                Response.Redirect(UIProcess.DEFAULT_CP_PAGE);
            }
        }
    }
    public class CommandItem
    {
        public CommandItem(string t, int n, bool _Visible)
        {
            this.Title = t;
            this.ID = n;
            this.IsVisible = _Visible;
        }
        private string title;
        public string Title
        {
            get { return this.title; }
            set { this.title = value; }
        }

        private int id;
        public int ID
        {
            get { return this.id; }
            set { this.id = value; }
        }

        private bool _isVisible;
        public bool IsVisible
        {
            get { return _isVisible; }
            set { this._isVisible = value; }
        }

        private int level;
        public int Level
        {
            get { return this.level; }
            set { this.level = value; }
        }
    }
}